<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            font-size: 14px;
        }
        .editor{
            width: 1000px;
            margin: 20px auto 0;
        }
        span{
            display: inline-block;
        }
        p{
            display: flex;
            flex-wrap: wrap;
        }
        .body{
            height: 500px;
            box-shadow: 0 0 5px rgba(0,0,0,0.3);
        }
        .handle{
            display: flex;
        }
        button{
            border: none;
            line-height: 24px;
            padding: 0 10px;
            margin-right: 10px;
            cursor: pointer
        }
        .body div{
            display: flex;
            flex-wrap: wrap;
        }
        font[size='1']{
            font-size: 12px;
        }
        font[size='2']{
            font-size: 14px;
        }
        font[size='3']{
            font-size: 16px;
        }
        font[size='4']{
            font-size: 18px;
        }
        font[size='5']{
            font-size: 20px;
        }
        font[size='6']{
            font-size: 24px;
        }
        font[size='7']{
            font-size: 38px;
        }
    </style>
</head>
<body>
    <div class="editor">
        <div class="handle">
            <button id="bold">加粗</button>
            <button id="italic">斜体</button>
            <button id="underline">下划线</button>
            <button id="strikeThrough">删除线</button>
            <button id="color">字体颜色</button>
            <button id="hiliteColor">背景颜色</button>
        </div>
        <div class="body" contenteditable="true" spellcheck='false'>
            <div>
                <p>我就来测试一下的，</p>
                <p>我就来测试一下的，</p>
                <p>我就来测试一下的，</p>
                <p>我就来测试一下的，</p>
                <p>我就来测试一下的。</p>
            </div>
        </div>
    </div>
    <script>
        document.execCommand('styleWithCSS', null, true);
        function getId(id){
            return document.getElementById(id);
        }
        getId("bold").addEventListener('click',function(){
            document.execCommand('bold',true);
        },false)
        getId("italic").addEventListener('click',function(){
            document.execCommand('italic',true);
        },false)
        getId("underline").addEventListener('click',function(){
            document.execCommand('underline',true);
        },false)
        getId("strikeThrough").addEventListener('click',function(){
            document.execCommand('strikeThrough',true);
        },false)
        getId("color").addEventListener('click',function(){
            document.execCommand('foreColor',true,'red');
        },false)
        getId("hiliteColor").addEventListener('click',function(){
            document.execCommand('hiliteColor',true,'#666666');
            document.execCommand('foreColor',true,'#ffffff');
            document.execCommand('fontSize',true,10);
        },false)
    </script>
</body>
</html>